package com.shamar.blog.web.backend;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shamar.blog.dto.BlogCategoryExecution;
import com.shamar.blog.entity.BlogCategory;
import com.shamar.blog.enums.BlogCategoryStateEnum;
import com.shamar.blog.service.BlogCategoryService;
import com.shamar.blog.util.CodeUtil;
import com.shamar.blog.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * @author fxm
 * @date 2020-08-02 20:50
 * @desc 后台分类控制器
 */
@Controller
@RequestMapping("/admin")
public class CategoryController {
	@Autowired
	public BlogCategoryService blogCategoryService;
	/**
	 * 获取所有分类数据
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/listcategory")
	@ResponseBody
	public ModelAndView listCategory(HttpServletRequest request) {
		//定义返回值
		ModelAndView  model  =  new ModelAndView("backend/category_list");
		try {
			BlogCategory blogCategoryCondition = new BlogCategory();
			BlogCategoryExecution blogCategoryExecution = blogCategoryService.getBlogCategoryList(blogCategoryCondition,0,100);
			model.addObject("blogCategoryList",blogCategoryExecution.getBlogCategoryList());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return model;
	}

	@PostMapping(value = "/getTopcategory")
	@ResponseBody
	public Map<String,Object> getTopCategory(HttpServletRequest request){
		//定义返回值
		Map<String,Object> modelMap = new HashMap<>();
		try{
			//一级分类
			List<BlogCategory> topCategory = blogCategoryService.selectTopCategory();
			modelMap.put("success",true);
			modelMap.put("topCategory",topCategory);
			return modelMap;
		}catch (Exception e){
			modelMap.put("success",false);
			modelMap.put("errMsg","数据库错误");
			return modelMap;
		}
	}


	/**
	 * 渲染修改分类页面
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/updatecategory")
	@ResponseBody
	private ModelAndView updateCategory(HttpServletRequest request) {
		//定义返回值
		return new ModelAndView("backend/update_category");
	}
	/**
	 * 接收前端传过来的数据,更新数据库中的分类记录
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/doupdatecategory")
	@ResponseBody
	private Map<String,Object> doUpdateCategory(HttpServletRequest request){
		//定义返回值
		Map<String,Object> modelMap = new HashMap<>();
//		Object currentBlogAdminObj = request.getSession().getAttribute("currentBlogAdmin");
//        若session中没有数据,则未登录
//		if (currentBlogAdminObj == null){
//			modelMap.put("success",false);
//			modelMap.put("errMsg","未登录");
//		}
//		else{
			ObjectMapper mapper = new ObjectMapper();
			BlogCategory blogCategory;
			String categoryStr = HttpServletRequestUtil.getString(request, "BlogCategoryStr");
			//以上接收前端传递过来的信息,下面使用jackson json databind转换为BlogCategory实体类
			try {
				blogCategory = mapper.readValue(categoryStr, BlogCategory.class);
				if (Objects.isNull(blogCategory)) {
					modelMap.put("success", false);
					modelMap.put("errMsg", "参数数据错误,请稍后重试");
					return modelMap;
				}
				blogCategory.setLastEdittime(new Date());
			}catch (Exception e){
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
				return modelMap;
			}
			Integer affectedNum = blogCategoryService.updateBlogCategory(blogCategory);
			if (affectedNum > 0){
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("errMsg", "数据库错误");
			}
		return modelMap;
	}

	@GetMapping(value = "/addcategory")
	public String addCategory(){
		return "backend/category_add";
	}


	@RequestMapping(value = "/doaddcategory", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> doaddcategory(HttpServletRequest request){
		//定义返回值
		Map<String, Object> modelMap = new HashMap<>();
		BlogCategory blogCategory = null;
		if (!CodeUtil.checkVerifyCode(request)) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "验证码输入错误");
			return modelMap;
		}
		ObjectMapper mapper = new ObjectMapper();
		String blogCategoryshopStr = HttpServletRequestUtil.getString(request, "BlogCategoryStr");
		//1.以上接收前端传递过来的信息,下面使用jackson json databind转换为shop实体类
		try {
			//获取前端传来的数据
			blogCategory = mapper.readValue(blogCategoryshopStr, BlogCategory.class);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
			return modelMap;
		}
		//2. 新增分类
//		if (blogCategory != null) {
		if (blogCategory != null) {
				BlogCategoryExecution bue = null;
				try {
//					调用Service层
					bue = blogCategoryService.insertSelective(blogCategory);
					if (bue.getState() == BlogCategoryStateEnum.NORMAL.getState()) {
						modelMap.put("success", true);
//						// 若shop创建成功，则加入session中，作为权限使用

					} else {
						modelMap.put("success", false);
						modelMap.put("errMsg", bue.getStateInfo());
					}
				} catch (Exception e) {
					modelMap.put("success", false);
//					modelMap.put("errMsg", e.getMessage());
					modelMap.put("errMsg", "出错");
					return modelMap;
				}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "请输入用户信息");
		}
		return modelMap;
	}
}
